package com.df.tv.launcher.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.df.tv.launcher.R;

/**
 * 首页联网状态View
 * 
 * @author John
 */
public class HomeNetworkingStatusView extends FrameLayout {

    private ImageView nerworking_status;
    private ConnectivityManager connmanager;

    public HomeNetworkingStatusView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public HomeNetworkingStatusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HomeNetworkingStatusView(Context context) {
        super(context);
        initView(context);
    }

    public void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.nerworking_status_view, null);
        // 联网状态的图片
        nerworking_status = (ImageView) view.findViewById(R.id.nerworking_status);
        addView(view);
        // 判断当前是否联网了
        QueryNetworkStatus(context);

        receiver = new NetstateReceiver();
        filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        //		context.registerReceiver(receiver, filter);
        //		context.unregisterReceiver(receiver);
    }

    // 订阅广播
    public void NetWorkingRegisterReceiver(Context context) {
        context.registerReceiver(receiver, filter);
    }

    // 取消广播
    public void NetWorkingUnregisterReceiver(Context context) {
        context.unregisterReceiver(receiver);
    }

    public void QueryNetworkStatus(Context context) {
        connmanager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connmanager.getActiveNetworkInfo();
        if (info == null) {
            nerworking_status.setBackgroundResource(R.drawable.off_network);
        }
        if (info != null) {
            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                nerworking_status.setBackgroundResource(R.drawable.connect_network);
            }
            else if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
                nerworking_status.setBackgroundResource(R.drawable.connect_network);
            }
            else {
                nerworking_status.setBackgroundResource(R.drawable.off_network);
            }
        }
    }

    private static final int CHECK_NETWORK_STATE = 0;
    Handler handle = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case CHECK_NETWORK_STATE:
                HomeNetworkingStatusView.this.QueryNetworkStatus(HomeNetworkingStatusView.this.getContext());
                break;
            default:
                break;
            }
        }

    };
    private IntentFilter filter;
    private NetstateReceiver receiver;

    public class NetstateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            handle.removeMessages(CHECK_NETWORK_STATE);
            handle.sendEmptyMessageDelayed(CHECK_NETWORK_STATE, 500);
        }
    }
}
